package com.atguigu.schedule.controller;

import com.atguigu.schedule.common.Result;
import com.atguigu.schedule.service.VerificationCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 验证码控制器
 * 
 * @author atguigu
 * @since 2024
 */
@RestController
@RequestMapping("/api/auth")
@CrossOrigin(origins = "*")
public class VerificationCodeController {
    
    @Autowired
    private VerificationCodeService verificationCodeService;
    
    /**
     * 生成验证码
     */
    @PostMapping("/generate-code")
    public Result<Map<String, Object>> generateCode(@RequestBody Map<String, String> request) {
        String phone = request.get("phone");
        return verificationCodeService.generateCode(phone);
    }
    
    /**
     * 验证验证码
     */
    @PostMapping("/verify-code")
    public Result<Boolean> verifyCode(@RequestBody Map<String, String> request) {
        String phone = request.get("phone");
        String code = request.get("code");
        
        return verificationCodeService.verifyCode(phone, code);
    }
    
    /**
     * 获取待处理验证码列表（管理员接口）
     */
    @GetMapping("/pending-codes")
    public Result<List<Map<String, Object>>> getPendingCodes() {
        return verificationCodeService.getPendingCodes();
    }
    
    /**
     * 清理过期验证码（管理员接口）
     */
    @PostMapping("/clean-expired-codes")
    public Result<String> cleanExpiredCodes() {
        verificationCodeService.cleanExpiredCodes();
        return Result.ok("过期验证码清理完成");
    }
}
